import org.molamil.acto.core.Logger;
import org.molamil.acto.pages.PageBuilder;
import org.molamil.acto.pages.PageContext;
import org.molamil.acto.pages.factory.PageFactory;

/**
 * Offers a basic implementation of <code>PageFactory</code>.
 * 
 * @author Jorge Hernandez
 */
class org.molamil.acto.pages.factory.BasicPageFactory implements PageFactory {
	
	
	// Properties
	
	private static var log:Logger = Logger.getLogger("BasicPageFactory");
	
	private var _list:Array;
	
	
	// Constructor
	
	/**
	 * Creates an empty BasicPageFactory.
	 */
	public function BasicPageFactory(list:Array) {
		log.debug("Constructor");
		if (list != null) {
			_list = list;
		} else {
			_list = new Array();
		}
	}
	
	
	// Functions
	
	/**
	 * Checks that the page exists.
	 * 
	 * @param   name Name of the page.
	 * @return  Whether the page exists or not.
	 */
	public function containsPage(name:String):Boolean {
		var b:Boolean = getPage(name) != null;
		log.debug("containsPage(), name = " + name + ": " + b);
		return b;
	}
	
	/**
	 * Gets all pages.
	 *    
	 * @return  An array containing all pages as <code>PageContext</code>
	 */
	public function getPages():Array {
		log.info("getPages(), returning " + _list.length);
		return _list;
	}
	
	/**
	 * Gets pages at a specific level.
	 *    
	 * @param	level Level to return pages from, page levels start at 0.
	 * @return  An array containing all pages fount at the specified level.
	 */
	public function getPagesAt(level:Number):Array {
		
		log.info("getPagesAt(), level = " + level);
		
		var a:Array = new Array();
		
		for (var i:Number = 0; i < _list.length; i++) {
			var pt:PageContext = _list[i];
			if (pt.level == level)
				a.push(pt);
		}
					
		return a;
		
	}
	
	/**
	 * Gets a page by name.
	 *    
	 * @param   name Page name.
	 * @return  If found, a <code>PageContext</code> object 
	 * 			(<code>ExternalPageMaster</code>, <code>InternalPageMaster</code> or <code>LinkedPageMaster</code>).	
	 * 			If not found, <code>null</code>.
	 */
	public function getPage(name:String):PageContext {
		
		log.info("getPage(), name = " + name);
		
		var p:PageContext = null;
		
		for (var i:Number = 0; i < _list.length; i++) {
			var pt:PageContext = _list[i];
			if (pt.name == name) {
				p = pt;
				break;
			}
		}
		
		if (p == null)
			log.warning("getPage(), name = " + name + " not found");
			
		return p;
		
	}
	
	/**
	 * Gets an <code>ExternalPageMaster</code> by name.
	 *    
	 * @param   name Page name.
	 * @return  If found, an <code>ExternalPageMaster</code> object. 
	 * 			If not found, <code>null</code>.
	 */
	public function getExternalPage(name:String):PageContext {
		log.info("getExternal(), name = " + name);
		var p:PageContext = getPage(name);
		if (p.type == PageBuilder.TYPE_EXTERNAL)
			log.warning("getExternal(), name = " + name + " not found");
		return p;
	}
	
	/**
	 * Gets an <code>InternalPageMaster</code> by name.
	 *    
	 * @param   name Page name.
	 * @return  If found, an <code>InternalPageMaster</code> object. 
	 * 			If not found, <code>null</code>.
	 */
	public function getInternalPage(name:String):PageContext {
		log.info("getInternal(), name = " + name);
		var p:PageContext = getPage(name);
		if (p.type == PageBuilder.TYPE_INTERNAL)
			log.warning("getInternal(), name = " + name + " not found");
		return p;
	}
	
	/**
	 * Gets an <code>LinkedPageMaster</code> by name.
	 *    
	 * @param   name Page name.
	 * @return  If found, an <code>LinkedPageMaster</code> object. 
	 * 			If not found, <code>null</code>.
	 */
	public function getLinkedPage(name:String):PageContext {
		log.info("getLinked(), name = " + name);
		var p:PageContext = getPage(name);
		if (p.type == PageBuilder.TYPE_LINKED)
			log.warning("getLinked(), name = " + name + " not found");
		return p;
	}
	
	
}